package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.RoleListDao;
import person.aoao.library.dao.UserListDao;
import person.aoao.library.entity.RoleList;
import person.aoao.library.entity.UserList;
import person.aoao.library.service.RoleListService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RoleList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-07 23:00:09
 */
@Service("roleListService")
public class RoleListServiceImpl implements RoleListService {
    @Resource
    private RoleListDao roleListDao;

    @Resource
    private UserListDao userListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param roleid 主键
     * @return 实例对象
     */
    @Override
    public RoleList queryById(Integer roleid) {
        return this.roleListDao.queryById(roleid);
    }


    /**
     * 分页查询
     *
     * @param roleList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<RoleList> queryByPage(RoleList roleList, PageRequest pageRequest) {
        long total = this.roleListDao.count(roleList);
        return new PageImpl<>(this.roleListDao.queryAllByLimit(roleList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param roleList 实例对象
     * @return 实例对象
     */
    @Override
    public RoleList insert(RoleList roleList) {
        this.roleListDao.insert(roleList);
        return roleList;
    }

    /**
     * 修改数据
     *
     * @param roleList 实例对象
     * @return 实例对象
     */
    @Override
    public RoleList update(RoleList roleList) {
        this.roleListDao.update(roleList);
        return this.queryById(roleList.getRoleid());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleid) {
        List<UserList> list=this.userListDao.searchByRoleid(roleid+"");
        if(list.size()>0){
            return false;
        }
        return this.roleListDao.deleteById(roleid) > 0;
    }

    @Override
    public List<RoleList> getRoleList() {
        return  this.roleListDao.getRoleList();
    }

    @Override
    public List<RoleList> searchRole(String value) {
        return  this.roleListDao.searchRole("%"+value+"%");
    }
}
